Component({
    /**
     * 组件的属性列表
     */
    properties: {
        position: {
            type: Object,
            value: {
                left: 0,
                right: 0,
                bottom: 0,
                top: 0
            } // left、top、right、bottom
        },
        left: {
            type: Number,
            value: 0
        },
        right: {
            type: Number,
            value: 0
        },
        top: {
            type: Number,
            value: 0
        },
        bottom: {
            type: Number,
            value: 0
        },
        h_margin: { // 水平方向（左右边距）
            type: Number,
            value: 20
        },
        v_margin: { // 垂直方向边距(上下边距)
            type: Number,
            value: 20
        }
    },
    data: {
        x: 345,
        y: 300,
        windowWidth: wx.getSystemInfoSync().windowWidth,
        windowHeight: wx.getSystemInfoSync().windowHeight,
        elementWidth: 0,
        elementHeight: 0
    },
    lifetimes: {
        attached() {
            // 初始化位置
            wx.createSelectorQuery().in(this).select('.com-float-box').boundingClientRect().exec((res) => {
                console.log(res);
                this.data.elementWidth = res[0] && res[0].width;
                this.data.elementHeight = res[0] && res[0].height;
                if (this.properties.position.left || this.properties.left) {
                    this.data.x = this.properties.position.left || this.properties.left;
                }
                if (this.properties.position.right || this.properties.right) {
                    this.data.x = this.data.windowWidth - this.data.elementWidth - (this.properties.position.right ? this.properties.position.right : this.properties.right);
                }
                if (this.properties.position.bottom || this.properties.bottom) {
                    this.data.y = this.data.windowHeight - this.data.elementHeight - (this.properties.position.bottom ? this.properties.position.bottom : this.properties.bottom);
                }
                if (this.properties.position.top || this.properties.top) {
                    this.data.y = this.properties.position.top || this.properties.top;
                }
                this.setData({
                    elementWidth: this.data.elementWidth,
                    elementHeight: this.data.elementHeight,
                    x: this.data.x,
                    y: this.data.y
                });
            });
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onTouchend(e) {
            // const currentX = e.changedTouches[0].clientX;
            // let currentY = e.changedTouches[0].clientY;
            // if (currentY <= this.properties.v_margin) {
            //     currentY = this.properties.v_margin;
            // }
            // if (currentY > this.data.windowHeight - this.properties.v_margin) {
            //     currentY = this.data.windowHeight - this.properties.v_margin;
            // }
            // if (currentX + this.data.elementWidth / 2 > this.data.windowWidth / 2) {
            //     this.setData({
            //         x: this.data.windowWidth - this.properties.h_margin - this.data.elementWidth,
            //         y: currentY - this.data.elementHeight / 2 - 10
            //     });
            // }
            // if (currentX + this.data.elementWidth / 2 <= this.data.windowWidth / 2) {
            //     this.setData({
            //         x: this.properties.h_margin,
            //         y: currentY - this.data.elementHeight / 2 - 10
            //     });
            // }
            this.triggerEvent('touchWifi');
        }
    }
});